package com.atguigu.gulimall.order.interceptor;

import cn.hutool.core.text.AntPathMatcher;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Component
public class LoginUserInterCeptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match("/order/order/status/**", uri);
        boolean match1 = antPathMatcher.match("/payed/notify", uri);
        boolean adminOrder = antPathMatcher.match("/order/order/list", uri);
        System.out.println(uri);
        if (match || match1 || adminOrder) {
            return true;
        }
        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (!Objects.isNull(attribute)) {
            loginUser.set(attribute);
            return true;
        } else {
            System.err.println("未登入，跳转到登入...");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }

    }
}
